package cn.charmaine.org.controller;

import cn.charmaine.basic.util.AjaxResult;
import cn.charmaine.basic.util.PageList;
import cn.charmaine.org.domain.SystemDictionaryType;
import cn.charmaine.org.query.SystemDictionaryTypeQuery;
import cn.charmaine.org.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dictionary")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    //分页+高级查询
    @PostMapping
    public PageList<SystemDictionaryType> queryData(@RequestBody SystemDictionaryTypeQuery query){
        PageList<SystemDictionaryType> list = systemDictionaryTypeService.queryData(query);
        System.out.println(list);
        return systemDictionaryTypeService.queryData(query);
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try{
            systemDictionaryTypeService.deleteById(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            /*return new AjaxResult(false,"操作失败！");*/
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }

    //新增+修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType dictionary){
        try{
            if(dictionary.getId() == null){//新增
                systemDictionaryTypeService.add(dictionary);
            }else{//添加
                systemDictionaryTypeService.update(dictionary);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            /*return new AjaxResult(false,"操作失败");*/
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }

}
